#!/bin/env bash 
# Xglom launcher
# A multi-modal kidney image analysis toolkit
#
#   - Michael Eager,   (michael.eager@monash.edu) 
#   - Monash Biomedical Imaging, Monash University, Australia

#     Copyright © 2012-2013 Michael Eager <michael.eager@monash.edu>
#
#     This file is part of Xglom.
# 
#     This is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
# 
#     This is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

_TMP_NIFTI_PATH="~/Monash038/NIFTI_20121012"

export XGLOM_VERSION=1.9.0


if [ ! -f ~/.Xglomrc ]; then

  # echo " Forcing MATLAB to use a larger memory block. This is necessary for the VM GUI GlomViewer."
  ## This hack will disrupt power users' settings - only do it to first time users
  sed -i '/JavaMemHeapMax/d' ~/.matlab/R2011a/matlab.prf
  echo "JavaMemHeapMax=I24115" >> ~/.matlab/R2011a/matlab.prf

  # Write Xglom config file
cat << EOF > ~/.Xglomrc
export NIFTI_PATH=${_TMP_NIFTI_PATH}
export XGLOM_LOCAL_CACHE="~/Monash038/Cache"
export XGLOM_USER_CACHE="~/Monash038/${USER}"
EOF

fi

source ~/.Xglomrc

if [ ! -d ${NIFT_PATH} ]; then
  echo "NIFTI path not found. Change settings in ~/.Xglomrc or contact administrator to change default settings in xglom.sh."
  export NIFTI_PATH=${_TMP_NIFTI_PATH}
fi


if [ ! -f ~/.daris ]; then
cat << EOF >  ~/.daris
mf-erc:mf-erc.its.monash.edu.au:https:8443:monash-ldap:AuthcateUserName:MBI-TOAND:MON-DARIS:6666
EOF
fi


# make sure users in group and others can read and write files created within Xglom
umask 0002  

XGLOMDIR=$(dirname $0)
cd $XGLOMDIR
# force group members to create files
chmod g+x $XGLOMDIR
# mkdir -p Cache
# find Cache -type d -exec chmod 775 {} \;
# find Cache -type f -exec chmod 644 {} \;

module load matlab virtualgl


echo " 
      Xglom -- The Glomeruli Explorer --  version ${XGLOM_VERSION}                        
  A multi-modal kidney image analysis MATLAB toolkit used for ultra-high 
  field MR, synchrotron CT, and virtual microscopy images. Xglom was produced 
  by Michael Eager at Monash Biomedical Imaging, Monash University, Australia			      
  for Professor John Bertram's Lab in the Department of Anatomy and Developmental
  Biology.  The project was funded by ANDS.

  Home page for project: https://confluence-vre.its.monash.edu.au/display/MKIA/
  Source code: https://code.google.com/p/xglom/	      	

  Copyright (C) 2012-2013, Michael Eager (michael.eager@monash.edu)	

  This program is free software: you can redistribute it and/or modify   
  it under the terms of the GNU General Public License as published by   
  the Free Software Foundation, either version 3 of the License, or	    
  (at your option) any later version.				    
							    
  This program is distributed in the hope that it will be useful,	    
  but WITHOUT ANY WARRANTY; without even the implied warranty of	   
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	    
  GNU General Public License for more details.			    
"

vglrun matlab -nodesktop -r 'h = Xglom;uiwait(h);quit' 

clear
reset
